package com.work.lxykeeper.module.pagemine;

import android.view.View;

import com.work.lxykeeper.R;
import com.work.lxykeeper.base.BaseActivity;
import com.work.lxykeeper.bean.PhoneCheck;
import com.work.lxykeeper.bean.WeixinData;
import com.work.lxykeeper.databinding.ActivityPagemineForgetpasswordBinding;
import com.work.lxykeeper.httpretrofit.MyCallback;
import com.work.lxykeeper.httpretrofit.PostBody;
import com.work.lxykeeper.httpretrofit.RetrofitManager;
import com.work.lxykeeper.utils.JsonUtils;
import com.work.lxykeeper.utils.UIUtils;
import com.work.lxykeeper.utils.VerificationUtil;
import com.work.lxykeeper.utils.ViewUtils;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;

/**
 * author: Aly
 * date :2018/12/7 13:32
 * version:1.0
 * description:绑定手机号
 */
public class BindPhoneActivity extends BaseActivity {
    private String data;
    private WeixinData weixinData;
    private ActivityPagemineForgetpasswordBinding binding;

    @Override
    protected View getLayoutView() {
        binding = ActivityPagemineForgetpasswordBinding.inflate(getLayoutInflater());
        return binding.getRoot();
    }

    @Override
    protected void initView() {
        mTitleBar.titleMiddle.setText("绑定手机号");
        binding.ltHelp.setVisibility(View.GONE);
        ViewUtils.setOnClickListeners(this, binding.cbtTime, binding.tvNext, binding.tvTel);
    }

    @Override
    protected void initData() {
        data = getIntent().getStringExtra("data");
        weixinData = JsonUtils.parseJsonToBean(data, WeixinData.class);
    }

    @Override
    protected void processClick(View v) {
        super.processClick(v);
        switch (v.getId()) {
            case R.id.cbt_time:
                //获取短信验证码
                if (VerificationUtil.isPhone(binding.etPhone.getText().toString())) {
                    getHttpCode(binding.etPhone.getText().toString());
                    binding.cbtTime.start();
                }
                break;
            case R.id.tv_next:
                //下一步
                getHttpchecksmscode();

//                UIUtils.startActivity(ResetPasswordActivity.class);
                break;
        }
    }

    //获取验证码
    private void getHttpCode(String phone) {
        Map<String, String> map = new HashMap<>();
        map.put("user_phone", phone);
        Call<Object> call = RetrofitManager.getInstance().getsmscode(PostBody.toBody(map));
        call.enqueue(new MyCallback<Object>() {
            @Override
            public void onSuccess(Object response) {
                UIUtils.showToast("验证码发送成功");
            }

            @Override
            public void onFailure(int errorCode, String errMsg) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    //校验验证码
    private void getHttpchecksmscode() {
        showLoaddialog();
        final String phone = binding.etPhone.getText().toString();
        Map<String, String> map = new HashMap<>();
        map.put("user_phone", phone);
        map.put("smscode", binding.etPhonecode.getText().toString());
        Call<PhoneCheck> call = RetrofitManager.getInstance().checksmscode(PostBody.toBody(map));
        call.enqueue(new MyCallback<PhoneCheck>() {
            @Override
            public void onSuccess(PhoneCheck response) {
                if (response.getErrcode() == 0) {
                    //验证码校验成功
//                    wechatblind();
                } else {
                    UIUtils.showToast("验证码验证失败");
                    hideLoaddialog();
                }

            }


            @Override
            public void onFailure(int errorCode, String errMsg) {
                UIUtils.showToast("errMsg");
                hideLoaddialog();
            }

            @Override
            public void onComplete() {

            }
        });
    }

//    //绑定微信
//    private void wechatblind() {
//        showLoaddialog();
//        Map<String, String> map = new HashMap<>();
//        map.put("phone_type", "Android");
//        map.put("open_id", weixinData.getOpenid());
//        map.put("user_picture", weixinData.getIcon());
//        map.put("nickname", weixinData.getNickname());
//        map.put("bindingPhone", etPhone.getText().toString());
//        Call<UserMessage> call = RetrofitManager.getInstance().wechatblind(PostBody.toBody(map));
//        call.enqueue(new MyCallback<UserMessage>() {
//            @Override
//            public void onSuccess(UserMessage response) {
//                if (response.getErrcode() == 0) {
//                    SPutils.put(Ckey.USERMESSAGE, JsonUtils.parseBeantojson(response));
//                    SPutils.put(Ckey.USERID, response.getData().getUser_id());
//                    UIUtils.startActivity(MainActivity.class);
//                    finish();
//                } else {
//                    UIUtils.showToast(response.getMsg());
//                }
//            }
//
//            @Override
//            public void onFailure(int errorCode, String errMsg) {
//
//            }
//
//            @Override
//            public void onComplete() {
//                hideLoaddialog();
//            }
//        });
//
//    }
}
